﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Xml.Serialization;
using System.IO;

namespace Base
{
    public class BaseFunctional
    {
        private IPEndPoint ipEndPoint;

        public BaseFunctional()
        {
            ipEndPoint = null;
        }

        public IPEndPoint GetEndPoint()
        {
            return ipEndPoint;
        }

        /// <summary>
        /// Create new Socket by EndPoint
        /// </summary>
        /// <param name="EPoint">EndPoint to connect</param>
        /// <returns></returns>
        public Socket CreateSocket(IPEndPoint EPoint)
        {
            if (EPoint != null)
            {
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                return sock;
            }
            else return null;
        }

        /// <summary>
        /// Create new Socket by IPAdress and int PORT
        /// </summary>
        /// <param name="IP">IPAdress of EndPoint</param>
        /// <param name="PORT">number of port</param>
        /// <returns></returns>
        public Socket CreateSocket(IPAddress IP, int PORT)
        {
            if (IP != null)
            {
                ipEndPoint = new IPEndPoint(IP, PORT);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                return sock;
            }
            else return null;
        }

        /// <summary>
        /// Create new Socket by string IP and int PORT
        /// </summary>
        /// <param name="IP">string of IPAdress</param>
        /// <param name="PORT">number of port</param>
        /// <returns></returns>
        public Socket CreateSocket(string IP, int PORT)
        {
            if (IP != null && PORT > 1)
            {
                IPHostEntry ipHost = Dns.GetHostEntry(IP);
                IPAddress ipAddr = ipHost.AddressList[0];
                ipEndPoint = new IPEndPoint(ipAddr, PORT);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                return sock;
            }
            else return null;
        }

        /// <summary>
        /// Create new Socket by int PORT to first IPAdress of localhost IPAdresses
        /// </summary>
        /// <param name="PORT">number of port</param>
        /// <returns></returns>
        public Socket CreateSocket(int PORT)
        {
            if (PORT >= 1)
            {
                IPHostEntry ipHost = Dns.GetHostEntry("127.0.0.1");
                IPAddress ipAddr = ipHost.AddressList[0];
                ipEndPoint = new IPEndPoint(ipAddr, PORT);
                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                return sock;
            }
            else return null;
        }

        /// <summary>
        /// ShutDowns Socket
        /// </summary>
        /// <param name="sock"></param>
        public void ShutDownSocket(Socket sock)
        {
            if (sock != null)
            {
                sock.Shutdown(SocketShutdown.Both);
                sock.Close();
            }
            return;
        }

        /// <summary>
        /// Serialize Message object to bytes array
        /// </summary>
        /// <param name="mes">Massege object</param>
        /// <returns></returns>
        public byte[] Serialization(Message mes)
        {
            if (mes != null)
            {
                StringWriter writer = new StringWriter();
                XmlSerializer serializer = new XmlSerializer(typeof(Message));
                serializer.Serialize(writer, mes);
                string str = writer.ToString();
                byte[] buf = new byte[str.Length];
                buf = Encoding.UTF8.GetBytes(writer.ToString());
                return buf;
            }
            else return null;
        }

        /// <summary>
        /// Deserialize bytes array to Message object
        /// </summary>
        /// <param name="buf">bytes array</param>
        /// <returns></returns>
        public Message DeSerialization(byte[] buf)
        {
            if (buf != null)
            {
                string str = null;
                str = Encoding.UTF8.GetString(buf);
                XmlSerializer serializer = new XmlSerializer(typeof(Message));
                StringReader reader = new StringReader(str);
                Message mes = (Message)serializer.Deserialize(reader);
                return mes;
            }
            else return null;
        }
    }
}
